package testgrails

/*
B-1: Bids have the following required fields: amount and date/time of bid (unit test)
B-2: Bids are required to be for a Listing (unit test)
B-3: Bids are required to have a bidder (Customer) (unit test)
B-4: A Listing has a list of Bids for that Listing (unit test)
*/

import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */

//	L-1: Listings have the following required fields: name, end date/time, and starting bid price (unit test)
//	L-2: Listings have the following optional fields: description (unit test)
//	L-3: Listings are required to have a seller (Customer) (unit test)
//	L-4: Listing descriptions must be less than 256 characters (unit test)
//	L-5: Listing end date/time must be in the future (unit test)
//	L-6: Listing name must be less than 64 characters (unit test)
//	L-7: Listing has a nullable field for the winner (Customer) (unit test)
//

@TestFor(Listing)
class ListingTests {

	def listing
	
	//L-1: Listings have the following required fields: name, end date/time, and starting bid price (unit test)
    void testRequiredFields() {
     listing = new Listing(name: null, endDate: null, startingBidPrice: null)
	 
	 assertFalse(listing.validate(['name']))
	 assertFalse(listing.validate(['endDate']))
	 assertFalse(listing.validate(['startingBidPrice']))
	 
//	 assert "nullable" == listing.errors['name'].code
//	 assert "nullable" == listing.errors['endDate'].code
//	 assert "nullable" == listing.errors['startingBidPrice'].code
    }
	
	//	L-2: Listings have the following optional fields: description (unit test)
	void testOptionalField() {
	 listing = new Listing(description: null)
	 assertNull(listing.description)
	 assertTrue(listing.validate(['description']))
	 
	 listing.description = "test"
	 assertTrue(listing.validate(['description']))
	}
	
	//	L-3: Listings are required to have a seller (Customer) (unit test)
	void testRequiredSeller() {
		listing = new Listing(name:'name1',endDate:new Date() + 1,startingBidPrice: 100.0)
		assertNull(listing.seller)
		assertFalse (listing.validate())
		
		def customer = new Customer(emailAddress : 'bushrat@gmail.com', password: 'password', dateCreated: new Date())
		listing = new Listing(name:'name1',endDate:new Date() + 1,startingBidPrice: 100.0, seller: customer)
		assertNotNull(listing.seller)
		assertTrue (listing.validate())
	   }
	
	//	L-4: Listing descriptions must be less than 256 characters (unit test)
	void testDescriptionSize() {
		listing = new Listing()
		listing.description = 'testsd'
		assertTrue(listing.validate(['description']))
		
		listing.description = '12345678912345678912345678testsdasgasgaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaattttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttassssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssasasasasas'
		assertFalse(listing.validate(['description']))
		
	}
	
	//	L-5: Listing end date/time must be in the future (unit test)
	void testListingEndDateTime() {
		listing = new Listing();
		listing.endDate = new Date()
		assertFalse(listing.validate(['endDate']));
		
		listing.endDate = new Date() + 1;
		assertTrue(listing.validate(['endDate']));
	}
	
	//	L-6: Listing name must be less than 64 characters (unit test)
	void testListingNameSize() {
		listing = new Listing()
		listing.name = 'test name test test'
		assertTrue(listing.validate(['name']))
		
		listing.name = 'test name test test name test test name test test name test test name test test name test test name test'
		assertFalse(listing.validate(['name']))
	}
	
	//	L-7: Listing has a nullable field for the winner (Customer) (unit test)
	void testNullableListingWinner() {
		listing = new Listing()
		def winner = null
		listing.winner = winner
		assertNull(listing.winner)
	
	}

	//B-2: Bids are required to be for a Listing (unit test)
	void testRequiredBid()
	{
		listing = new Listing(name:'test', endDate: new Date() + 1, startingBidPrice: 1.0, bids: null)
		assertFalse (listing.validate())
	}
	

}
